﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Net;
using System.Xml;

namespace FanTong
{    
    public partial class FrmLogin : Form
    {
        public OperationINI.rwini ini = new OperationINI.rwini(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName)+"\\Config.ini");
        public Microsoft.WindowsCE.Forms.InputPanel inputpanel = new Microsoft.WindowsCE.Forms.InputPanel();
        public FrmLogin()
        {
            InitializeComponent();
            inputpanel.Enabled = false;
            chkClosOrOpen.Checked = false;
            bool remember =  bool.Parse(ini.IniReadValue("UserConfig", "Remember"));
            if (remember)
            {
                chkRemember.Checked = remember;
                txtName.Text = ini.IniReadValue("UserConfig", "UserName");
                txtPassword.Text =new EncryptionDecryption().decode(ini.IniReadValue("UserConfig", "UserPassWord"));
            }
            this.Deactivate+= new EventHandler(this.FrmLogin_Deactivate);
            //ini.writeint("", "isShow", 1);
            //int isShow = ini.getint("","isShow",0);
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim().Length == 0)
            {
                labError.Text = "用户名不能为空,请输入用户名。";
                labError.Visible = true;
                return;
            }
            if (txtName.Text.Trim().Length == 0)
            {
                labError.Text = "密码不能为空,请输入密码。";
                labError.Visible = true;
                return;
            }
            labError.Text ="登录中,请稍候...";
            labError.Visible = true;
            inputpanel.Enabled = false;
            btnLogin.Enabled = false;            
            chkRemember.Enabled = false;
            txtPassword.Enabled = false;
            txtName.Enabled = false;
            chkClosOrOpen.Enabled = false;
            if (checkLogin())
            {
                ini.IniWriteValue("UserConfig", "UserName", txtName.Text);
                //Encoder(password, "abcdefgh");
                //Decrypt(password, "abcdefgh");
                ini.IniWriteValue("UserConfig", "UserPassWord", new EncryptionDecryption().encode(txtPassword.Text));
                if (chkRemember.Checked)
                {
                    ini.IniWriteValue("UserConfig", "Remember", chkRemember.Checked.ToString());
                }
                this.Deactivate -= new EventHandler(this.FrmLogin_Deactivate);
                MainForm main = new MainForm();
                main.Show();
                this.Hide();
            }
            else
            {
                labError.Text = "用户名或密码错误,请重新输入.";
                labError.Visible = true;
                btnLogin.Enabled = true;                
                chkRemember.Enabled = true;
                txtPassword.Enabled = true;
                txtName.Enabled = true;
                chkClosOrOpen.Enabled = true;
            }
        }

        private void txtName_GotFocus(object sender, EventArgs e)
        {
            labError.Visible = false;
            inputpanel.Enabled = true;
            chkClosOrOpen.Checked = true;
        }

        private void txtName_LostFocus(object sender, EventArgs e)
        {
            inputpanel.Enabled = false; 
            chkClosOrOpen.Checked = false;
        }

        private void chkClosOrOpen_Click(object sender, EventArgs e)
        {
            if (this.chkClosOrOpen.Checked)
            {
                inputpanel.Enabled = false;
                chkClosOrOpen.Checked = false;
            }
            else
            {
                inputpanel.Enabled = true;
                chkClosOrOpen.Checked = true;
            }
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            string isShow = ini.IniReadValue("config", "isShow");
            if (isShow == "1")
            {
                MainForm main = new MainForm();
                main.Show();
                this.Hide();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {   
            Application.Exit();
        }

        private void FrmLogin_Deactivate(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public bool checkLogin()
        {
            bool isReturn = false;
            DateTime date = DateTime.Now;
            string erroMsg = "";
            MSXML2.XMLHTTPClass xmlhttp = new MSXML2.XMLHTTPClass();
        //http://api.fanfou.com/account/verify_credentials
            //string strxml = XMLMethod.sendRequest("http://api.fanfou.com/account/verify_credentials.xml", true, txtName.Text.Trim(), txtPassword.Text.Trim(), ref date, ref erroMsg).xml;
            //string strxml = XMLMethod.sendRequest("http://api.fanfou.com/statuses/friends.xml?id=" + UserName, true, UserName, Password, ref nodate, ref ErrorMsg).xml;
            xmlhttp.open("POST", "http://api.fanfou.com/account/verify_credentials.xml", false, txtName.Text.Trim(), txtPassword.Text.Trim());
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlhttp.setRequestHeader("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(txtName.Text.Trim() + ":" + txtPassword.Text.Trim())));
            xmlhttp.send("");
            System.Xml.XmlDocument xmldom = new System.Xml.XmlDocument();
            xmldom.LoadXml(xmlhttp.responseText);
            System.Xml.XmlNodeList objNodes;
            objNodes = xmldom.SelectNodes("user");
            if (objNodes.Count > 0)
                isReturn = true;
            
            return isReturn;
        }
    }
}